package com.ruoyi.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.VideoConstants;
import com.ruoyi.edu.api.domain.EduChapter;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.edu.api.domain.EduVideo;
import com.ruoyi.edu.mapper.EduVideoMapper;
import com.ruoyi.edu.service.IEduChapterService;
import com.ruoyi.edu.service.IEduVideoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 课程视频Service业务层处理
 *
 * @author isahino
 * @date 2023-09-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements IEduVideoService {
    @Resource
    private EduVideoMapper eduVideoMapper;
    @Resource
    private IEduChapterService iEduChapterService;

    /**
     * 查询课程视频
     *
     * @param id 课程视频主键
     * @return 课程视频
     */
    @Override
    public EduVideo selectEduVideoById(String id) {
        return eduVideoMapper.selectEduVideoById(id);
    }

    /**
     * 查询课程视频列表
     *
     * @param eduVideo 课程视频
     * @return 课程视频
     */
    @Override
    public List<EduVideo> selectEduVideoList(EduVideo eduVideo) {
        return eduVideoMapper.selectEduVideoList(eduVideo);
    }

    /**
     * 新增课程视频
     *
     * @param eduVideo 课程视频
     * @return 结果
     */
    @Override
    public int insertEduVideo(EduVideo eduVideo) {
        setEduVideoData(eduVideo);
        return eduVideoMapper.insert(eduVideo);
    }

    /**
     * 修改课程视频
     *
     * @param eduVideo 课程视频
     * @return 结果
     */
    @Override
    public int updateEduVideo(EduVideo eduVideo) {
        setEduVideoData(eduVideo);
        return eduVideoMapper.updateById(eduVideo);
    }

    /**
     * 为视频所属课程、状态赋值
     * @param eduVideo 视频
     */
    private void setEduVideoData(EduVideo eduVideo) {
        // 设置视频所属课程id
        eduVideo.setCourseId(iEduChapterService.getById(eduVideo.getChapterId()).getCourseId());
        // 根据视频资源的存在与否修改状态
        if ("".equals(eduVideo.getVideoSourceId())) {
            eduVideo.setStatus(VideoConstants.UN_UPLOAD);
        } else {
            eduVideo.setStatus(VideoConstants.UPLOAD_DONE);
        }
    }
    /**
     * 批量删除课程视频
     *
     * @param ids 需要删除的课程视频主键
     * @return 结果
     */
    @Override
    public int deleteEduVideoByIds(String[] ids) {
        return eduVideoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除课程视频信息
     *
     * @param id 课程视频主键
     * @return 结果
     */
    @Override
    public int deleteEduVideoById(String id) {
        return eduVideoMapper.deleteById(id);
    }
}
